package com.wjw.java.others.string_;

/**
 * 字符串反转：
 */
public class StringReverse {
    public static void main(String[] args) {
        String str = "abcdef";
        System.out.println("交换前："+str);
        try {
            str = reverse(str, 0, 4);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return;
        }
        System.out.println("交换后:"+str);

        System.out.println("---------------------------------------------------------");
        try {
            userRegister("wjw","123456","285324520@qq.com");
            userRegister("","","");
        } catch (Exception e) {
            //System.out.println(e.getMessage());
            e.printStackTrace();
        }
        System.out.println("---------------------------------------------------------");
    }

    /**
     * 字符串反转
     * @param str
     * @param start
     * @param end
     * @return
     */
    public static String reverse(String str,int start,int end){
        /**
         * 重要编程技巧：
         * (1)先写出正确的情况
         * (2)然后取反即可(及不满足条件的判断)
         */
        if(!(str != null &&
           !"".equals(str) &&
           start >= 0 &&
           start < end &&
           end < str.length())){
            throw new RuntimeException("参数不匹配");
        }
        //1.字符串转为字符数组
        char [] chars = str.toCharArray();
        char temp = ' ';
        for (int i = start,j = end; i < j; i++,j--){
            temp = chars[i];
            chars[i] = chars[j];
            chars[j]  = temp;
        }
        return new String(chars);
    }

    /**
     * 用户注册模拟,失败抛出异常对象
     * @param username      长度2、3、4
     * @param password      长度要求为6,全数字
     * @param email         包含@符号,且@符号在.的前面
     */
    public static void userRegister(String username,String password,String email){
        if(!(isNotNull(username) && isNotNull(password) && isNotNull(email))){
            throw new RuntimeException("用户名、密码、邮箱不能为空");
        }
        if(!(username.length() >= 2 && username.length() <= 4)){
            throw new RuntimeException("用户名长度必须是：2、3、4");
        }
        if(!(password.length() == 6 && isDigital(password))){
            throw new RuntimeException("密码长度要求为6,全数字");
        }
        int a = email.indexOf("@");
        int b = email.indexOf(".");
        if(!(a > 0 && a < b)){//之所以要a > 0是不让@符号在第一位
            throw new RuntimeException("邮箱包含@符号,且@符号在.的前面");
        }
        System.out.println("注册成功:"+"用户名"+username+",密码"+password+",邮箱"+email);
    }

    /**
     * 判断传入的字符串是否都是数字
     * @param str
     * @return
     */
    public static boolean isDigital(String str){
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i] < '0' || chars[i] > '9'){
                return false;
            }
        }
        return true;
    }

    /**
     * 判断字符串是否为空：不为空true,为空false
     * @param str
     * @return
     */
    public static boolean isNotNull(String str){
        if(str != null && !"".equals(str)){
            return true;
        }
        return false;
    }
}
